<?php
/* 
    Project     : 48c6c450f1a4a0cc53d9585dc0fee742
    Created on  : Mar 16, 2013, 11:29:15 PM
    Author      : Truong Khuong - khuongxuantruong@gmail.com
    Description :
        Purpose of the stylesheet follows.
*/
class category_model extends Backend_Model {

    function __construct()
    {
        parent::__construct('categories','cate');
		
    }
    function gets($type=null){
        if($type!=null)$this->db->where('cate_type',$type);
        $query=$this->db
                ->select('cate_title as text,cate_id as value,cate_parent as parentid,cate_id as id')
                ->order_by('cate_parent', 'ASC')
                ->order_by('cate_position', 'ASC')
                ->order_by('cate_insert', 'DESC')
                ->get('categories');
        return $query->result();
    }
    function treebinding($type=''){
        if($type!=null)$this->db->where('cate_type',$type);
        $query=$this->db
                ->select('cate_title as text,cate_id as value,cate_parent as parentid,cate_id as id')
                ->order_by('cate_parent', 'ASC')
                ->order_by('cate_position', 'ASC')
                ->order_by('cate_insert', 'DESC')
                ->get('categories');
        return $query->result();
    }
    function get_by_type($type=''){
        if($type!=null)$this->db->where('cate_type',$type);
        $query=$this->db
            //->select('cate_id,cate_title,cate_parent,cate_position')
            ->order_by('cate_parent','ASC')
            ->order_by('cate_position','ASC')
            ->order_by('cate_insert','DESC')
            ->get("categories"); 
        return $query->result();
    }
    function get_in_types($types = null) {
        if ($types != null){
            if(!is_array($types))$types=array($types);
            $this->db->where_in('cate_type', $types);
        }
        $query = $this->db
                ->order_by('cate_parent', 'ASC')
                ->order_by('cate_position', 'ASC')
                ->order_by('cate_insert', 'DESC')
                ->get("categories");
        //die($this->db->last_query());
        return $query->result();
    }
    
    function binding($type=null){
        //$position=isset($_SESSION["auth"]["user"])?$_SESSION["auth"]["user"]->ause_position:0;
        $strQuery="
            SELECT SQL_CALC_FOUND_ROWS `categories`.*,cate_id as _id
            FROM `categories`
                
            ";
        $strWhere="
            WHERE  true
            ";
        
        if(!empty($type)){$type=  $this->db->escape_str($type);$strWhere.=" AND `cate_type` = '$type'";}
        if($_SESSION["ADP"][CCTRL]["display"]===0){
            $strWhere.=" AND `cate_delete` IS NULL";
        }elseif($_SESSION["ADP"][CCTRL]["display"]===-1){
            $strWhere.=" AND `cate_delete` IS NOT NULL";
        }
        $strOrderBy="ORDER BY `cate_position` ASC,`cate_insert` ASC ";
        $strGroupBy="";
        $config=array(
            "strQuery"=>$strQuery,
            "strWhere"=>$strWhere,
            "strOrderBy"=>$strOrderBy,
            "strGroupBy"=>$strGroupBy,
            "usingLimit"=>false,
            "fields"=>array(
                'cate_display_title'=>'cate_title',
                'cate_parent_title'=>'cate_parent'
                ),
            "datefields"=>array()
        );
        $this->init($config);
        return $this->jqxBinding();
    }
    function buildTree(array $elements, $parentId = 0) {
        $branch = array();
        foreach ($elements as $element) {
            if ($element->cate_parent == $parentId) {
                $children = $this->buildTree($elements, $element->cate_id);
                if ($children) {
                    $element->children = $children;
                }
                $branch[] = $element;
            }
        }
        return $branch;
    }
    function buildTreeArray(array $elements, $parentId = 0,$level=0,$parent_title='') {
        $branch = array();
        foreach ($elements as $element) {
            if ($element->cate_parent == $parentId) {
                $element->cate_level=$level;$element->cate_parent_title=$parent_title;
                $element->cate_display_title=repeater('----',$level).$element->cate_title;
                $branch[] = $element;
                $children = $this->buildTreeArray($elements, $element->cate_id,$level+1,$element->cate_title);
                if (!empty($children))foreach ($children as $ch){
                    $branch[] = $ch;
                }
            }
        }
        return $branch;
    }
}
class category_basemodel extends Backend_Model {
    public $catetory_list;
    function __construct($type=null)
    {
        parent::__construct('categories','cate');
        $this->cate_type=$type;
        if($type!=null)$this->hierarchy();
    }
    function treebinding($type=''){
        if($type!=null)$this->db->where('cate_type',$type);
        $query=$this->db
                ->select('cate_title as text,cate_id as value,cate_parent as parentid,cate_id as id')
                ->order_by('cate_parent', 'ASC')
                ->order_by('cate_position', 'ASC')
                ->order_by('cate_insert', 'DESC')
                ->get('categories');
        return $query->result();
    }
    function _gets(){
        return $this->get_by_type($this->cate_type);
    }
    function get_by_type($type=''){
        if($type!=null)$this->db->where('cate_type',$type);
        $query=$this->db
            //->select('cate_id,cate_title,cate_parent,cate_position')
            ->order_by('cate_parent','ASC')
            ->order_by('cate_position','ASC')
            ->order_by('cate_insert','DESC')
            ->get("categories"); 
        return $query->result();
    }
    function get_in_types($types = null) {
        if ($types != null){
            if(!is_array($types))$types=array($types);
            $this->db->where_in('cate_type', $types);
        }
        $query = $this->db
                ->order_by('cate_parent', 'ASC')
                ->order_by('cate_position', 'ASC')
                ->order_by('cate_insert', 'DESC')
                ->get("categories");
        return $query->result();
    }
    function binding($type=null){
        //$position=isset($_SESSION["auth"]["user"])?$_SESSION["auth"]["user"]->ause_position:0;
        $strQuery="
            SELECT SQL_CALC_FOUND_ROWS c.*,c.cate_id as _id
                ,(SELECT b.cate_title from `categories` b where c.cate_parent = b.cate_id limit 1) as cate_parent_title
            FROM `categories` c
                
            ";
        $strWhere="
            WHERE  true
            ";
        if(!empty($type)){$type=  $this->db->escape_str($type);$strWhere.=" AND c.cate_type = '$type'";}
        if($_SESSION["ADP"][CCTRL]["display"]===0){
            $strWhere.=" AND c.cate_delete IS NULL";
        }elseif($_SESSION["ADP"][CCTRL]["display"]===-1){
            $strWhere.=" AND c.cate_delete IS NOT NULL";
        }
        $strOrderBy="ORDER BY c.cate_parent, c.cate_position, c.cate_insert DESC ";
        $strGroupBy="";
        $config=array(
            "strQuery"=>$strQuery,
            "strWhere"=>$strWhere,
            "strOrderBy"=>$strOrderBy,
            "strGroupBy"=>$strGroupBy,
            "usingLimit"=>false,
            "fields"=>array(),
            "datefields"=>array()
        );
        $this->init($config);
        return $this->jqxBinding();
    }
    function colmod(){
        return array(
            array(  
                "text"          =>"#"            
                ,"datafield"    =>"_id" 
                //,"cellsalign"   =>"right"
                ,"width"        => 60 
                ,"filterable"       =>false
                ),
            array(  
                "text"          =>"ID"            
                ,"datafield"    =>"cate_id" 
                //,"cellsalign"   =>"right"
                ,"width"        => 60 
                //,"hidden"       =>true
                ),
            array(  
                "text"          =>"Thumb"            
                ,"datafield"    =>"cate_thumb" 
                //,"cellsalign"   =>"right"
                ,"width"        => 60 
                ,"filterable"       =>false
                ),
            array(  
                "text"          =>"Title"            
                ,"datafield"    =>"cate_title" 
                //,"width"        => 220
                ,"editable"     =>true
                ),
             array(  
                "text"          =>"Alias"            
                ,"datafield"    =>"cate_alias" 
                ,"width"        => 120
                ,"editable"     =>false
                ),
            array(  
                "text"          =>"Position"            
                ,"datafield"    =>"cate_position" 
                ,"width"        => 80
                ,'type'         =>'number'
                ,'columntype'   =>'numberinput'
                ,"filtertype"   =>"number"  //textbox,checkedlist,list,number,checkbox,date
                //,"cellsformat"  =>'n'
                ,"cellsalign"   =>"right"
                ,"editable"     =>true
                ),
            array(  
                "text"          =>"Parent ID"            
                ,"datafield"    =>"cate_parent" 
                ,"width"        => 80
                ,"editable"     =>false
                ,"hidden"       =>true
                ),
            array(  
                "text"          =>"Parent"            
                ,"datafield"    =>"cate_parent_title" 
                ,"width"        => 120
                ,"editable"     =>false
                ,"filterable"   =>false
                ),
            array(  
                "text"          =>"Type"            
                ,"datafield"    =>"cate_type" 
                //,"cellsalign"   =>"right"
                ,"width"        => 100 
                //,"hidden"       =>true
                ),
            array(  
                "text"          =>"Status"            
                ,"datafield"    =>"cate_status" 
                ,"type"         =>"bool"
                ,"width"        => 80   
                ,"columntype"   =>"checkbox"
                ,"threestatecheckbox"=> false
                ,"filtertype"   =>"bool"  
                ,"editable"     =>true
                ),
            array(  
                "text"          =>"Insert"            
                ,"datafield"    =>"cate_insert" 
                ,"width"        => 120    
                ,"cellsformat"  =>'yyyy-MM-dd HH:mm:ss'
                ,"hidden"       =>true
                ),
            array(  
                "text"          =>"Update"            
                ,"datafield"    =>"cate_update" 
                ,"width"        => 120    
                ,"cellsformat"  =>'yyyy-MM-dd HH:mm:ss'
                ,"hidden"       =>true
                ),
            array(  
                "text"          =>"Delete"            
                ,"datafield"    =>"cate_delete" 
                ,"width"        => 120    
                ,"cellsformat"  =>'yyyy-MM-dd HH:mm:ss'
                ,"hidden"       =>true
                )
        );
    }
    function get_child(){
        
    }
    function hierarchy($cate_parent=0){
        if(!isset($this->cats)){
            $this->cats=$this->get_by_type($this->cate_type);
            foreach ($this->cats as $c){
                $c->aparents=array(0);
                $c->achilds=array();
                $c->path=$c->cate_alias;
                $c->pathname=$c->cate_title;
                $this->catetory_list[$c->cate_id]=$c;
            }
        }
        $childs=array();$achilds=null;
        foreach ($this->cats as $c){
            if($c->cate_parent==$cate_parent){
                if(isset($this->catetory_list[$cate_parent])){
                    $this->catetory_list[$c->cate_id]->level=
                            $this->catetory_list[$c->cate_id]->cate_level=
                            $c->level=
                            $this->catetory_list[$cate_parent]->level+1;
                    $this->catetory_list[$c->cate_id]->path=
                            $c->path=
                            $this->catetory_list[$cate_parent]->path."/".$c->cate_alias;
                    $this->catetory_list[$c->cate_id]->pathname=
                            $c->pathname=
                            $this->catetory_list[$cate_parent]->pathname."/".$c->cate_title;
                    $this->catetory_list[$c->cate_id]->aparents=
                            array_unique(
                                    array_merge(
                                            $this->catetory_list[$cate_parent]->aparents,
                                            array($cate_parent)
                                    )
                            );
                }else{
                    $c->aparents=array(0);
                    $c->achilds=array();
                    $c->path=$c->cate_alias;
                    $c->pathname=$c->cate_title;
                    $c->level=$c->cate_level=1;
                }
                $achilds[]=$c->cate_id;
                array_push($childs, $c);
                $this->hierarchy($c->cate_id);
            }
        }
        if(isset($this->catetory_list[$cate_parent])){
            $this->catetory_list[$cate_parent]->childs=$childs;
            if($achilds)
            $this->catetory_list[$cate_parent]->achilds=
                    array_unique(
                            array_merge(
                                    $this->catetory_list[$cate_parent]->achilds,
                                    $achilds
                            )
                    );
            foreach($this->catetory_list[$cate_parent]->aparents as $p){
                if(isset($this->catetory_list[$p]) && $achilds){
                    $this->catetory_list[$p]->achilds=
                        array_unique(
                                array_merge(
                                        $this->catetory_list[$p]->achilds,
                                        $achilds
                                )
                        );
                }
            }
            
        }
    }
}
?>
